package com.csii.adhoc.controller.board;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csii.adhoc.board.BoardPo;
import com.csii.adhoc.board.service.BoardService;
import com.csii.adhoc.controller.dataReady.TreeConverter;
import com.csii.adhoc.dataready.TMgnDir;
import com.csii.adhoc.dataready.TMgnDirPo;
import com.csii.adhoc.board.service.DashBoardService;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.common.constant.CommonConstant;
import com.csii.adhoc.controller.BaseController;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: 仪表盘目录维护
 * @author: liuzhonghua
 * @create:
 */
@RestController
@RequestMapping("/dashBoard")
@Slf4j
public class DashBoardController extends BaseController<TMgnDir, DashBoardService> {
    @Resource
    private DashBoardService dashBoardService;
    @Resource
    private BoardService boardService;

    @RequestMapping("dashBoardMaintain")
    public APIResult<?> dashBoardMaintain(@RequestBody TMgnDirPo vo) {
        //获取当前时间存入创建时间中
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        //当前日期
        String format = df.format(new Date());
        String userId = String.valueOf(SecurityUtils.getSubject().getPrincipal());
        vo.setCreatedBy(userId);
        vo.setCreatedDt(format);
        vo.setUpdatedBy(vo.getCreatedBy());
        vo.setUpdatedDt(vo.getCreatedDt());
        TMgnDir tMgnDir = ConvertUtils.convert(TMgnDir.class, vo);
        switch (vo.getOptMode()){
            case "A":
                dashBoardService.save(tMgnDir);
                boardService.addBoardFromDir(vo);
                return APIResult.successResult("添加成功！");
            case "U":
                BoardPo boardPo = new BoardPo();
                boardPo.setBoardId(vo.getMenuId()).setName(vo.getName());
                dashBoardService.updateById(tMgnDir);
                boardService.updateBoardPoByPrimaryKey(boardPo);
                return APIResult.successResult("修改成功！");
            case "D":
                boardPo = new BoardPo();
                boardPo.setBoardId(vo.getMenuId());
                boardService.boardMaintain(boardPo, CommonConstant.OPT_MODE_DELETE);
                dashBoardService.removeById(tMgnDir.getMenuId());
                return APIResult.successResult("删除成功！");
            default:
                log.error("optMode值不合法:" + vo.getOptMode());
                throw new ParamValidityException("操作标志不合法:" + vo.getOptMode());
        }
    }
    @RequestMapping("queryDashBoard")
    public APIResult<?> queryDashBoard(@RequestBody TMgnDirPo vo){
        QueryWrapper<TMgnDir> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TYPE","6");
        if(!StringUtils.isEmpty(vo.getMenuId())) {
            queryWrapper.eq("PARENT_ID", vo.getMenuId());
        }
        if(!StringUtils.isEmpty(vo.getName())){
            queryWrapper.like("NAME",vo.getName());
        }
        List<TMgnDir> list = dashBoardService.list(queryWrapper);
        return APIResult.successResult(list);
    }

    @RequestMapping("queryDashBoardTree")
    public APIResult<?> queryDashBoardTree(@RequestBody TMgnDirPo vo){
        QueryWrapper<TMgnDir> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TYPE","6");
        if(!StringUtils.isEmpty(vo.getMenuId())) {
            queryWrapper.eq("PARENT_ID", vo.getMenuId());
        }
        if(!StringUtils.isEmpty(vo.getName())){
            queryWrapper.like("NAME",vo.getName());
        }
        List<TMgnDir> list = dashBoardService.list(queryWrapper);
        List<TMgnDir> tMgnDir = TreeConverter.convertToTree(list);
        return APIResult.successResult(tMgnDir);
    }



}
